﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
	using RDIFramework.Utilities;
	

	/// <summary>
	/// SequenceService
	/// 服务层
	/// 
	/// 修改记录
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class SequenceService : System.MarshalByRefObject, ISequenceService
	{
        private readonly string serviceName = RDIFrameworkMessage.SequenceService;

        /// <summary>
        /// 多用户并发处理用
        /// </summary>
        private static Object LOCK = new Object();

        /// <summary>
        /// .NET快速开发整合框架（RDIFramework.NET）数据库连接
        /// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        /// <summary>
        /// 添加序列
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="sequenceEntity">序列实体</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>主键</returns>
        public string Add(UserInfo userInfo, CiSequenceEntity sequenceEntity, out string statusCode, out string statusMessage)
        {
            string returnValue = string.Empty;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider, userInfo);
                    // 调用方法，并且返回运行结果
                    returnValue = sequenceManager.Add(sequenceEntity, out statusCode);
                    // returnValue = businessCardManager.Add(businessCardEntity, out statusCode);
                    statusMessage = sequenceManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_Add, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

        #region public string Add(UserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        /// <summary>
        /// 添加编码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="dataTable">数据表</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>数据表</returns>
        public string Add(UserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            CiSequenceEntity sequenceEntity = new CiSequenceEntity(dataTable);
            return this.Add(userInfo, sequenceEntity, out statusCode, out statusMessage);
        }
        #endregion

        #region public DataTable GetDT(UserInfo userInfo) 获取序列号列表
        /// <summary>
        /// 获取序列号列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetDT(UserInfo userInfo)
        {
            DataTable dataTable = new DataTable(CiSequenceTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                    dataTable = sequenceManager.GetDT();
                    dataTable.TableName = CiSequenceTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_GetDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return dataTable;
        }
        #endregion

        #region public CiSequenceEntity GetEntity(UserInfo userInfo, string id)
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public CiSequenceEntity GetEntity(UserInfo userInfo, string id)
        {    
            CiSequenceEntity sequenceEntity = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider, userInfo);
                    sequenceEntity = sequenceManager.GetEntity(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_GetEntity, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return sequenceEntity;
        }
        #endregion

        /// <summary>
        /// 更新序列
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="CiSequenceEntity">序列实体</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="statusMessage">状态信息</param>
        /// <returns>影响行数</returns>
        public int Update(UserInfo userInfo, CiSequenceEntity CiSequenceEntity, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;

            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider, userInfo);
                    // 编辑数据
                    returnValue = sequenceManager.Update(CiSequenceEntity, out statusCode);
                    // returnValue = businessCardManager.Update(businessCardEntity, out statusCode);
                    statusMessage = sequenceManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_Update, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        #region public int Update(UserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        /// <summary>
        /// 更新编码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="dataTable">数据表</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>数据表</returns>
        public int Update(UserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            CiSequenceEntity CiSequenceEntity = new CiSequenceEntity(dataTable);
            return this.Update(userInfo, CiSequenceEntity, out statusCode, out statusMessage);
        }
        #endregion

        #region public string GetSequence(UserInfo userInfo, string fullName) 获取序列号
        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <returns>序列号</returns>
        public string GetSequence(UserInfo userInfo, string fullName)
        {
            string returnValue = string.Empty;

            // 这里是为了防止并发冲突用的
            lock (LOCK)
            {
                using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
                {
                    try
                    {
                        dbProvider.Open(RDIFrameworkDbConection);
                        returnValue = this.GetSequence(dbProvider, userInfo, fullName);
                    }
                    catch (Exception ex)
                    {
                        CiExceptionManager.LogException(dbProvider, userInfo, ex);
                        throw ex;
                    }
                    finally
                    {
                        dbProvider.Close();
                    }
                }
            }
            return returnValue;
        }
        #endregion

        #region public string GetSequence(IDbProvider dbProvider, UserInfo userInfo, string fullName) 获取序列号
        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <returns>序列号</returns>
        public string GetSequence(IDbProvider dbProvider, UserInfo userInfo, string fullName)
        {
            CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
            return sequenceManager.GetSequence(fullName);
        }
        #endregion

        #region public string GetOldSequence(UserInfo userInfo, string fullName, int defaultSequence, int sequenceLength, bool fillZeroPrefix) 获取原序列号
        /// <summary>
        /// 获取原序列号
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <param name="defaultSequence">默认序列</param>
        /// <param name="sequenceLength">序列长度</param>
        /// <param name="fillZeroPrefix">是否填充补零</param>
        /// <returns>序列号</returns>
        public string GetOldSequence(UserInfo userInfo, string fullName, int defaultSequence, int sequenceLength, bool fillZeroPrefix)
        {
            string returnValue = string.Empty;

            // 这里是为了防止并发冲突用的
            lock (LOCK)
            {
                using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
                {
                    try
                    {
                        dbProvider.Open(RDIFrameworkDbConection);
                        CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                        returnValue = sequenceManager.GetOldSequence(fullName, defaultSequence, sequenceLength, fillZeroPrefix);
                    }
                    catch (Exception ex)
                    {
                        CiExceptionManager.LogException(dbProvider, userInfo, ex);
                        throw ex;
                    }
                    finally
                    {
                        dbProvider.Close();
                    }
                }
            }

            return returnValue;
        }
        #endregion

        #region public string GetNewSequence(UserInfo userInfo, string fullName, int defaultSequence, int sequenceLength, bool fillZeroPrefix) 获取原序列号
        /// <summary>
        /// 获取新序列号
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <param name="defaultSequence">默认序列</param>
        /// <param name="sequenceLength">序列长度</param>
        /// <param name="fillZeroPrefix">是否填充补零</param>
        /// <returns>序列号</returns>
        public string GetNewSequence(UserInfo userInfo, string fullName, int defaultSequence, int sequenceLength, bool fillZeroPrefix)
        {
            string returnValue = string.Empty;

            // 这里是为了防止并发冲突用的
            lock (LOCK)
            {
                using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
                {
                    try
                    {
                        dbProvider.Open(RDIFrameworkDbConection);
                        CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                        returnValue = sequenceManager.GetSequence(fullName, defaultSequence, sequenceLength, fillZeroPrefix);
                    }
                    catch (Exception ex)
                    {
                        CiExceptionManager.LogException(dbProvider, userInfo, ex);
                        throw ex;
                    }
                    finally
                    {
                        dbProvider.Close();
                    }
                }
            }
            return returnValue;
        }
        #endregion


        #region public string[] GetBatchSequence(UserInfo userInfo, string fullName, int count) 获取序列号
        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <param name="count">个数</param>
        /// <returns>序列号</returns>
        public string[] GetBatchSequence(UserInfo userInfo, string fullName, int count)
        {
            string[] returnValue = new string[0];

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    returnValue = this.GetBatchSequence(dbProvider, userInfo, fullName, count);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public string[] GetBatchSequence(IDbProvider dbProvider, UserInfo userInfo, string fullName, count) 获取序列号
        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <param name="count">个数</param>
        /// <returns>序列号</returns>
        public string[] GetBatchSequence(IDbProvider dbProvider, UserInfo userInfo, string fullName, int count)
        {
            CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
            return sequenceManager.GetBatchSequence(fullName, count);
        }
        #endregion

        #region public string GetReduction(UserInfo userInfo, string fullName) 获取倒序序列号
        /// <summary>
        /// 获取倒序序列号
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <returns>序列号</returns>
        public string GetReduction(UserInfo userInfo, string fullName)
        {
            string returnValue = string.Empty;

            // 这里是为了防止并发冲突用的
            lock (LOCK)
            {
                using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
                {
                    try
                    {
                        dbProvider.Open(RDIFrameworkDbConection);
                        returnValue = this.GetReduction(dbProvider, userInfo, fullName);
                    }
                    catch (Exception ex)
                    {
                        CiExceptionManager.LogException(dbProvider, userInfo, ex);
                        throw ex;
                    }
                    finally
                    {
                        dbProvider.Close();
                    }
                }
            }
            
            return returnValue;
        }
        #endregion

        #region public string GetReduction(IDbProvider dbProvider, UserInfo userInfo, string fullName)
        /// <summary>
        /// 获取倒序序列号
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户</param>
        /// <param name="fullName">序列名称</param>
        /// <returns>序列号</returns>
        public string GetReduction(IDbProvider dbProvider, UserInfo userInfo, string fullName)
        {
            CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
            return sequenceManager.GetReduction(fullName);
        }
        #endregion

        #region public int Reset(UserInfo userInfo, string[] ids) 批量重置
        /// <summary>
        /// 批量重置序列
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int Reset(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                    returnValue = sequenceManager.Reset(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_Reset, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public int Delete(UserInfo userInfo, string id) 删除序列
        /// <summary>
        /// 删除序列
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(UserInfo userInfo, string id)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                    returnValue = sequenceManager.Delete(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_Delete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public int BatchDelete(UserInfo userInfo, string[] ids) 批量删除序列
        /// <summary>
        /// 批量删除序列
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>数据表</returns>
        public int BatchDelete(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiSequenceManager sequenceManager = new CiSequenceManager(dbProvider);
                    returnValue = sequenceManager.Delete(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.SequenceService_BatchDelete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion
	}
}
